<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    td,
    tr
    {
        border: 1px solid;
    }
    body
    {
        display: flex;
        justify-content: center;

        align-items: center;
        height: 600px;

        flex-direction: column;
    }
</style>

<body>
    <label><h1>添加博客或者修改</h1></label>
    <table>
        <tr>
            <td><label for="">标题：</label></td>
            <td><input type="text" placeholder="请输入标题" name="name"></td>
        </tr>
        <tr>
            <td><label for="">摘要：</label></td>
            <td><input type="text" placeholder="请输入摘要" name="school"></td>
        </tr>
        <tr>
            <td><label for="">内容：</label></td>
            <td><input type="text" placeholder="请输入内容" name="money"></td>
        </tr>
        <tr>
            <td><label for="">作者</label></td>
            <td><input type="text" placeholder="请输入作者" name="grifriend"></td>
        </tr>
        <tr>
            <td><input type="button" value="保存" onclick="save()"></td>
            <td><input type="button" value="取消" onclick="cancel()"></td>
        </tr>
    </table>
    <script src="./js/jquery-3.6.0.min.js"></script>
    <script src="./config/baseUrl.js"></script>
    <script src="./js/api.js"></script>

    <script>
        $(function () {
            let id = getUrlParam('id');
            id = id || 0;
            console.log(id);
            getProductById(id).then(res => {
                console.log(res);
                let obj = res.data;
                $('[headline=headline]').val(obj.headline);
                $('[headline=abstract]').val(obj.abstract);
                $('[headline=content]').val(obj.content);
                $('[headline=classify]').val(obj.classify);
                $('[headline=author]').val(obj.author);
                $('[headline=time]').val(obj.time);
        
            })
        })
        //获取url中的参数
        function getUrlParam(headline) {

            var reg = new RegExp("(^|&)" + headline + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象

            var r = window.location.search.substr(1).match(reg); //匹配目标参数

            if (r != null) return unescape(r[2]); return null; //返回参数值

        }
        function save() {
            let id = getUrlParam('id');
            console.log(id);
            let obj = {
                headline: $('[headline=headline]').val(),
                abstract: $('[headline=abstract]').val(),
                content: $('[headline=content]').val(),
                classify:  $('[headline=classify]').val(),
                author: $('[headline=author]').val(),
                time: $('[headline=time]').val(),
        
            }
            // 如果id存在，则表示为编辑，否则为新增
            if (id) {
                updateProduct(id, obj).then(res => {
                    console.log(res);
                    location.href = './index.html';
                })
            } else {
                let params = {
                    headline: obj.headline,
                    abstract: obj.abstract,
                    content: obj.content,
                    classify: obj.classify,
                    author:obj.author,
                    time:obj.time
                }
                addProduct(params).then(res => {
                    location.href = './index.html';
                })
            }


        }
        function cancel() {
            location.href = './index.html';
        }
    </script>
</body>

</html>